package com.exam.service.impl;

import com.exam.mapper.FillQuestionMapper;
import com.exam.mapper.PaperMapMapper;
import com.exam.pojo.ChoiceQuestion;
import com.exam.pojo.ComposePaper;
import com.exam.pojo.FillQuestion;
import com.exam.pojo.PaperMap;
import com.exam.service.FillQuestionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class FillQuestionServiceImpl implements FillQuestionService {
    @Resource
    private FillQuestionMapper fillQuestionMapper;
    @Resource
    private PaperMapMapper paperMapMapper;
    @Override
    public List<FillQuestion> queryByPaperId(Integer paperId) {
        return fillQuestionMapper.queryByPaperId(paperId);
    }

    @Override
    public PageInfo<FillQuestion> queryPageByPaperId(Integer paperId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<FillQuestion> fillQuestions = fillQuestionMapper.queryByPaperId(paperId);
        PageInfo<FillQuestion> pageInfo = new PageInfo<>(fillQuestions);
        return pageInfo;
    }

    @Override
    public PageInfo<FillQuestion> queryPageByQuestionCreatorId(Integer questionCreatorId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<FillQuestion> fillQuestions = fillQuestionMapper.queryByQuestionCreatorId(questionCreatorId);
        PageInfo<FillQuestion> pageInfo = new PageInfo<>(fillQuestions);
        return pageInfo;
    }

    @Override
    public PageInfo<FillQuestion> queryAllPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<FillQuestion> fillQuestions = fillQuestionMapper.queryAll();
        PageInfo<FillQuestion> pageInfo = new PageInfo<>(fillQuestions);
        return pageInfo;
    }

    @Override
    public int updateById(FillQuestion fillQuestion) {
        if (fillQuestion.getScore() == null){
            fillQuestion.setScore(2);
        }
        return fillQuestionMapper.updateById(fillQuestion);
    }

    @Override
    @Transactional
    public void deleteById(Integer id) {
        int deleteById = fillQuestionMapper.deleteById(id);
        if (deleteById == 0) {
            throw new RuntimeException("删除填空题试题失败");
        }
        paperMapMapper.deleteByQuestionId(new PaperMap(null, 2, id));

    }

    @Override
    public int add(FillQuestion fillQuestion) {
        return fillQuestionMapper.insert(fillQuestion);
    }

    @Override
    public List<ComposePaper> querySubNumByQuestionCreatorId(Integer questionCreatorId) {
        return fillQuestionMapper.querySubNumByQuestionCreatorId(questionCreatorId);
    }

    @Override
    public List<ComposePaper> querySubNum() {
        return fillQuestionMapper.querySubNum();
    }
}
